package com.yxq.mdt.app.modules.auction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.hawk.Hawk;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.auction.bean.AuctionList;
import com.yxq.mdt.app.modules.auction.bean.AuctionProfitBean;
import com.yxq.mdt.app.modules.auction.bean.AuctionTxBean;
import com.yxq.mdt.app.modules.auction.model.AuctionModel;
import com.yxq.mdt.app.modules.auction.presenter.AuctionContract;
import com.yxq.mdt.app.modules.auction.presenter.AuctionPresenter;
import com.yxq.mdt.app.modules.main.bean.NameAndPhoneBean;
import com.yxq.mdt.app.modules.webview.ui.MiWebViewActivity;
import com.yxq.mdt.app.net.API;
import com.yxq.mdt.app.utils.CashierInputFilter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 提现
 */
public class AuctionCashAc extends BaseActivity<AuctionPresenter> implements AuctionContract.View {

    @BindView(R.id.draw_tv_balance)
    TextView drawTvBalance;
    @BindView(R.id.draw_tv_account)
    EditText drawTvAccount;
    @BindView(R.id.draw_tv_update)
    TextView drawTvUpdate;
    @BindView(R.id.login_tv_name)
    TextView loginTvName;
    @BindView(R.id.login_et_name)
    EditText loginEtName;
    @BindView(R.id.draw_et_before_money)
    EditText drawEtBeforeMoney;
    @BindView(R.id.draw_tv_after_money)
    TextView drawTvAfterMoney;
    @BindView(R.id.draw_tv_contact_phone)
    EditText drawTvContactPhone;
    @BindView(R.id.draw_btn_submit)
    Button drawBtnSubmit;
    @BindView(R.id.draw_checkbox)
    CheckBox drawCheckbox;
    @BindView(R.id.draw_tv_doc)
    TextView drawTvDoc;
    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.draw_tv_after_tips)
    ImageView drawTvAfterTips;

    @Override
    public int getLayoutId() {
        return R.layout.activity_auction_cash_withdrawal;
    }

    @Override
    public void initView() {
        mPresenter = new AuctionPresenter(this, new AuctionModel());
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter.getUserIncomeList(30, page, -1);
    }

    @Override
    public void onLoadAuctionListByTypeSuccess(AuctionList auctionList) {

    }

    @Override
    public void onLoadAuctionListByTypeFailed() {

    }

    @Override
    public void onLoadUserCashListSuccess(AuctionProfitBean auctionProfitBean) {

    }

    @Override
    public void onLoadUserCashListFailed() {

    }

    @Override
    public void onLoadUserIncomeListSuccess(AuctionTxBean auctionTxBean) {
        initView(auctionTxBean);
    }

    private void initView(AuctionTxBean auctionTxBean) {
        NameAndPhoneBean nameAndPhoneBean = Hawk.get(SPKey.SP_KEY_NAME_PHONE_INFO);
        drawTvBalance.setText(String.format("￥%.2f", auctionTxBean.getUsable_income()));

        InputFilter[] filters = {new CashierInputFilter()};
        drawEtBeforeMoney.setFilters(filters);

        drawEtBeforeMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (drawEtBeforeMoney.getText().length() > 0) {
                    if (Double.parseDouble(drawEtBeforeMoney.getText().toString()) >= 6d) {
                        double p = Double.parseDouble(drawEtBeforeMoney.getText().toString());
                        if (p > auctionTxBean.getUsable_income()) {
                            ToastUtils.showShort("提现数目不能大于余额");
//                            drawEtBeforeMoney.setText(auctionTxBean.getUsable_income() + "");
//                            drawEtBeforeMoney.setSelection((auctionTxBean.getUsable_income() + "").length());
//                            drawTvAfterMoney.setText(String.format("%.2f", auctionTxBean.getUsable_income() * (1 - nameAndPhoneBean.getPmSetting().getWithdraw_fee())));
                            setButtonEnable();
                            return;
                        }
                        drawTvAfterMoney.setText(String.format("%.2f", p * (1 - nameAndPhoneBean.getPmSetting().getWithdraw_fee())));
                    } else {
                        ToastUtils.showShort("请输入大于5元的金额");
                        drawTvAfterMoney.setText("0.0");
                        setButtonEnable();
                    }
                } else {
                    drawTvAfterMoney.setText("0.0");
                    setButtonEnable();
                }
            }
        });

        drawCheckbox.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!drawCheckbox.isChecked()) {
                ToastUtils.showShort("请先同意用户兑换协议");
            }
            setButtonEnable();
        });
    }

    /**
     * 控制按钮能否点击
     */
    private void setButtonEnable() {
        if (drawTvAfterMoney.getText().toString().length() != 0) {
            double p = Double.parseDouble(drawTvAfterMoney.getText().toString());
            if (drawCheckbox.isChecked() && p > 0d) {
                drawBtnSubmit.setEnabled(true);
            } else {
                drawBtnSubmit.setEnabled(false);
            }

        }
    }

    @Override
    public void onLoadUserIncomeListFailed() {

    }

    @Override
    public void onLoadAddUserCashSuccess(String result) {
        hideLoadingDialog();
        ToastUtils.showShort("申请提现成功");
        Intent profit = new Intent(mActivity, AuctionProfitListAc.class);
        startActivity(profit);
        finish();
    }

    @Override
    public void onLoadAddUserCashFailed(String msg) {
        hideLoadingDialog();
        ToastUtils.showShort(msg);
    }

    @OnClick({R.id.draw_btn_submit, R.id.draw_tv_doc, R.id.draw_tv_after_tips})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.draw_btn_submit:
                submit();
                break;
            case R.id.draw_tv_doc:
                Intent intent = new Intent(mActivity, MiWebViewActivity.class);
                intent.putExtra("url", API.CASH_WITHDRAWAL);
                startActivity(intent);
                break;
            case R.id.draw_tv_after_tips:
                showBottomToast("系统自动扣除税费5%,手续费0.6%.");
                break;
            default:
        }
    }

    /**
     * 提现
     */
    private void submit() {
        if (drawTvAccount.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入支付宝账号");
            return;
        }
        if (loginEtName.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入真实姓名");
            return;
        }
        if (drawEtBeforeMoney.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入提现金额");
            return;
        }
        if (Double.parseDouble(drawEtBeforeMoney.getText().toString()) < 6d) {
            ToastUtils.showShort("最低6元起提");
            return;
        }
        if (drawTvContactPhone.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入联系电话");
            return;
        }
        if (!drawCheckbox.isChecked()) {
            ToastUtils.showShort("请先同意用户兑换协议");
        }
        showLoadingDialog();
        mPresenter.addUserCash(drawTvAccount.getText().toString(), loginEtName.getText().toString(), drawEtBeforeMoney.getText().toString(), drawTvContactPhone.getText().toString());
    }

}
